﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;// 用到Registry和RegistryKey两个类  
using System.Timers;
using System.Threading;
using System.Reflection;
using System.IO;

using System.Runtime.InteropServices;

using Oracle.ManagedDataAccess.Client;


namespace ExportToDWGTool
{
    public partial class ExportToDWG : Form
    {

        private Teigha.Runtime.Services m_RServices = null;


        public ExportToDWG()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            m_RServices = new Teigha.Runtime.Services();
            // this.textBox1.Text = "SQL:10.10.100.161;DL_DYSJHCDataBase;sa;123456";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //OpenFileDialog openFileDlg = new OpenFileDialog();
            //openFileDlg.Multiselect = false;
            //openFileDlg.FileName = "";
            //openFileDlg.Filter = "源数据库（.mdb）" + "|*" + ".mdb";
            //if (openFileDlg.ShowDialog() == DialogResult.OK)
            //{
            //    this.textBox1.Text = openFileDlg.FileName;
            //}

            //DataBaseConnectFrm frm = new DataBaseConnectFrm();
            //frm.StartPosition = FormStartPosition.CenterParent;
            //if (frm.ShowDialog() == DialogResult.OK)
            //{
            //    this.textBox1.Text = "Oracle数据库";  //;"Oracle:" + frm.m_IP + ";" + frm.m_Port + ";" + frm.m_Server + ";" + frm.m_UserName + ";" + frm.m_PassWord;

            //}

            //****************************************************
            //泰州SQL
            //DataBaseConn frm = new DataBaseConn();
            //frm.StartPosition = FormStartPosition.CenterParent;
            //if (frm.ShowDialog() == DialogResult.OK)
            //{
            //    this.textBox1.Text = "SQL:" + frm.m_strIP + ";" + frm.m_strDB + ";" + frm.m_strUID + ";" + frm.m_strPWD;
            //}
            //*****************************************************
            //泰州Excel
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Multiselect = true;
            openFileDlg.FileName = "";
            openFileDlg.Filter = "Excel数据表（.xls）" + "|*" + ".xls";
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                string[] str = openFileDlg.FileNames;
                for (int i = 0; i < str.Length; i++)
                {
                    if (i == 0)
                    {
                        this.textBox1.Text = str[i];
                    }
                    else
                    {
                        this.textBox1.Text = this.textBox1.Text + ";" + str[i];
                    }
                }

            }

            //*****************************************************
            ////泰州Excel
            //OpenFileDialog openFileDlg = new OpenFileDialog();
            //openFileDlg.Multiselect = false;
            //openFileDlg.FileName = "";
            //openFileDlg.Filter = "Excel数据表（.xls）" + "|*" + ".xls";
            //if (openFileDlg.ShowDialog() == DialogResult.OK)
            //{
            //    string[] str = openFileDlg.FileNames;
            //    for (int i = 0; i < str.Length; i++)
            //    {
            //        if (i == 0)
            //        {
            //            this.textBox1.Text = str[i];
            //        }
            //        else
            //        {
            //            this.textBox1.Text = this.textBox1.Text + ";" + str[i];
            //        }
            //    }

            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.FileName = "";
            openFileDlg.Filter = "CAD图形模板（.DWG）" + "|*" + ".DWG";
            openFileDlg.Multiselect = false;
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = openFileDlg.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDlg = new SaveFileDialog();
            //saveFileDlg.Filter = "保存类型（.DWG）" + "|*" + ".DWG";
            //if (saveFileDlg.ShowDialog() == DialogResult.OK)
            //{
            //    this.textBox3.Text = saveFileDlg.FileName;
            //}

            FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
            if (folderBrowserDlg.ShowDialog() == DialogResult.OK)
            {
                this.textBox3.Text = folderBrowserDlg.SelectedPath;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
         
            if (this.textBox1.Text == null || textBox2.Text == null || textBox3.Text == null)
            {
                MessageBox.Show("请选择源数据和保存路径！", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            //ReadDataFromORCL read = new ReadDataFromORCL();
            //read.ReadJingTableData();

            //read.ReadDL_DLDGLBData();

            //CreateDWG create = new CreateDWG();

            //create.m_DicJ = read.m_DicJ;
            //create.m_dicDLDTD = read.m_dicDLDTD;
            //create.m_dicDLD = read.m_dicDLD;
            //create.m_DicSB = read.m_DicSB;
            //if (create.SaveToDWG(this.textBox2.Text, this.textBox3.Text))
            //{
            //    MessageBox.Show("转换完成！", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}

            //*******************************************
            //泰州SQL
            //string strSQL = textBox1.Text;
            //strSQL = strSQL.Substring(4, strSQL.Length - 4);
            //string[] strList = strSQL.Split(';');
            //ReadDataFromSQL readSQL = new ReadDataFromSQL(strList[0], strList[1], strList[2], strList[3]);
            //readSQL.GetDataFromTable();

            //ExportDWG dwg = new ExportDWG();
            //dwg.m_DicSB = readSQL.m_DicSB;
            //dwg.m_DicX = readSQL.m_DicX;
            //string strTempPath = this.textBox2.Text;
            //string strOutPath = this.textBox3.Text;
            //if (dwg.SaveToDWG(strTempPath, strOutPath))
            //{
            //    MessageBox.Show("转换完成！", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //*******************************************
            //泰州Excel 开关
            ReadDataFromExcelKG read = new ReadDataFromExcelKG();
            string str = this.textBox1.Text;
            string[] list = str.Split(';');
            for (int i = 0; i < list.Length; i++)
            {
                string strPath = list[i];
                if (strPath.Contains("杆塔信息"))
                {
                    string strFileName = strPath.Substring(strPath.LastIndexOf('\\') + 1, strPath.LastIndexOf('.') - strPath.LastIndexOf('\\') - 1);
                    read.m_strNM = strFileName.Substring(0, strFileName.Length - 4);
                    read.m_strBYQType = "柱上变";
                    read.ReadSBInfoExcel(strPath);
                }
                else if (strPath.Contains("用户信息"))
                {
                    read.ReadYHInfoExcel(strPath);
                }
                else if (strPath.Contains("分支箱信息"))
                {
                    read.m_strBYQType = "站内变";
                    read.ReadSBInfoExcel(strPath);
                }
            }

            string strTempPath = this.textBox2.Text;
            string strOutPath = this.textBox3.Text;
            //*******************************************
            //泰州Excel
            //ReadDataFromExcel read = new ReadDataFromExcel();
            //string str = this.textBox1.Text;
            //string[] list = str.Split(';');
            //for (int i = 0; i < list.Length; i++)
            //{
            //    string strPath = list[i];
            //    if (strPath.Contains("杆塔信息"))
            //    {
                    
            //        read.m_strBYQType = "柱上变";
            //        read.ReadSBInfoExcel(strPath);
            //    }
            //    else if (strPath.Contains("用户信息"))
            //    {
            //        read.ReadYHInfoExcel(strPath);
            //    }
            //    else if (strPath.Contains("分支箱信息"))
            //    {
            //        read.m_strBYQType = "站内变";
            //        read.ReadSBInfoExcel(strPath);
            //    }
            //}

            //string strTempPath = this.textBox2.Text;
            //string strOutPath = this.textBox3.Text;
            //**************************************************************
            ////中压Excel
            //ReadDataFromExcelZY read = new ReadDataFromExcelZY();
            //string str = this.textBox1.Text;
            //string[] list = str.Split(';');
            //for (int i = 0; i < list.Length; i++)
            //{
            //    string strPath = list[i];
            //    if (strPath.Contains("杆塔信息"))
            //    {

            //        read.m_strBYQType = "柱上变";
            //        read.ReadSBInfoExcel(strPath);

            //       // string strFileName = strPath.Substring(strPath.LastIndexOf('\\') + 1, strPath.LastIndexOf('.') - strPath.LastIndexOf('\\') - 1);
            //       // read.m_strNM = strFileName.Substring(0, strFileName.Length - 4);

            //    }
            //    //else if (strPath.Contains("用户信息"))
            //    //{
            //    //    read.ReadYHInfoExcel(strPath);
            //    //}
            //    //else if (strPath.Contains("分支箱信息"))
            //    //{
            //    //    read.m_strBYQType = "站内变";
            //    //    read.ReadSBInfoExcel(strPath);
            //    //}
            //}

            //string strTempPath = this.textBox2.Text;
            //string strOutPath = this.textBox3.Text;

            if (read.SaveToDWG(strTempPath, strOutPath))
            {
                MessageBox.Show("转换完成！", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
            m_RServices.Dispose();
            System.Environment.Exit(0);
        }

        private void ExportToDWG_FormClosed(object sender, FormClosedEventArgs e)
        {
            Dispose();
            m_RServices.Dispose();
            System.Environment.Exit(0);
        }


        /// <summary>  
        /// 修改程序在注册表中的键值  
        /// </summary>  
        /// <param name="isAuto">true:开机启动,false:不开机自启</param> 
        public void AutoStart(bool isAuto)
        {
            try
            {
                string fileName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);

                if (isAuto == true)
                {
                    RegistryKey R_local = Registry.LocalMachine;//RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.SetValue(fileName, Application.ExecutablePath);
                    R_run.Close();
                    R_local.Close();
                }
                else
                {
                    RegistryKey R_local = Registry.LocalMachine;//RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue(fileName, false);
                    R_run.Close();
                    R_local.Close();
                }

            }
            catch (Exception)
            {        
                MessageBox.Show("您需要管理员权限修改", "提示");
            }
        }

        bool bStop = true;

        private void ExportToDWG_Load(object sender, EventArgs e)
        {
             //AutoStart(true);

            ////定义线程   
            //Thread LogThread = new Thread(new ThreadStart(DoService));
            ////设置线程为后台线程,那样进程里就不会有未关闭的程序了
            //LogThread.IsBackground = true;
            //bStop = true;
            //if (bStop == true)
            //{
            //    LogThread.Start();//起线程
            //}

        }



        private bool ExportDWGFromExcel(string strExcel, string strTemp, string strOutDWG)
        {
            try
            {
                ReadDataFromExcel read = new ReadDataFromExcel();
                string str = strExcel;
                string[] list = str.Split(';');
                for (int i = 0; i < list.Length; i++)
                {
                    string strPath = list[i];
                    if (strPath.Contains("杆塔信息"))
                    {
                        read.ReadSBInfoExcel(strPath);
                    }
                    else if (strPath.Contains("用户信息"))
                    {
                        read.ReadYHInfoExcel(strPath);
                    }
                }

                string strTempPath = strTemp;
                string strOutPath = strOutDWG;

                if (read.SaveToDWG(strTempPath, strOutPath))
                {
                    return true;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }

        private delegate bool ExportDelegate(string strSQL, string strTemp, string strOutDWG);

        private void DoService()
        {
            while (true)
            {
                bStop = false;
                if (bStop == false)
                {
                    Thread.Sleep(5000);
                    continue;
                }
              
                returnList("","","", ExportDWGFromExcel);
            }
        }
       
        private bool returnList(string strSQL, string strTemp, string strOutDWG,ExportDelegate myDelegate)
        {
               return myDelegate(strSQL, strTemp, strOutDWG);          
        }

    }
}
